<script setup>
import {ref, reactive, onMounted} from 'vue'
import axios from "axios";
import {ElMessage, ElMessageBox} from 'element-plus'

// 分页配置
const pagination = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0
})

// 筛选表单
const filterForm = reactive({
  name: '',
  store: ''
})
// 表格数据
const tableData = ref([])
const stores = ref([])
const dialogVisible = ref(false)
const formModel = reactive({
  id: '',
  storeId:'',
  name: '',
  discountRate: 1,
  pointsRequired: 0,
  store: '',
  description: ''
})
// 获取门店列表
const fetchStores = async () => {
  axios.get('/member/stores').then((res) => {
    stores.value = res.data
  })
}
// // 综合查询方法
// const handleQuery = () => {
//   console.log('查询条件:', filterForm)
//   fetchData()
// }
// 获取数据
// const fetchData = () => {
//   console.log('获取数据')
//   axios().then((res) => {
//     tableData.value=res.data;
//   })
// }
// 数据查询
const fetchData = () => {
  axios.get(`/member/page/${pagination.pageNum}/${pagination.pageSize}`).then((res) => {
    tableData.value = res.data.data;
    pagination.total = res.data.total;
  })
}

// 添加表单
// 添加/编辑表单
const submitForm = (row) => {
  try {
    if (!formModel.id) {
      // 新增操作
      axios.post('/member/add', formModel).then((res) => {
        ElMessage.success('新增成功')
        dialogVisible.value = false
        fetchData()
      });
    } else {
      // 更新操作
      axios.post(`/member/update/${formModel.id}`, formModel).then((res) => {
        ElMessage.success('修改成功')
        dialogVisible.value = false
        fetchData()
      })
    }
  } catch (e) {
    ElMessage.error('操作失败')
  }
}

// 删除操作
const handleDelete = (row) => {
  try {
    ElMessageBox.confirm('确认删除该记录？', '警告', {type: 'warning'})
    axios.delete(`/member/${row.id}`).then((res) => {
      ElMessage.success('删除成功')
      fetchData()
    })
  } catch {
  }
}

onMounted(() => {
  fetchData()
  fetchStores()
})
</script>

<template>

  <div class="member-type-container">
    <!-- 顶部标题 -->
    <div class="header">
      <h1>会员类别</h1>
    </div>
    <!-- 筛选区域 -->
    <div class="filter-container">
      <el-form :inline="true" :model="filterForm" class="filter-form">
        <el-form-item label="会员类别">
          <el-input v-model="filterForm.name" placeholder="输入名称筛选"/>
        </el-form-item>

        <el-form-item label="请选择门店">
          <el-select v-model="filterForm.store" clearable>
            <el-option
                v-for="store in stores"
                :key="store"
                :label="store"
                :value="store"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="fetchData">查询</el-button>
          <el-button type="primary" @click="dialogVisible = true">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 数据表格 -->
    <div class="table-container">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="name" label="会员类别" width="150"/>
        <el-table-column prop="discountRate" label="折扣" width="100"/>
        <el-table-column prop="pointsRequired" label="消费金额" width="150"/>
        <el-table-column prop="store" label="所属门店" width="150"/>
        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <el-button type="text" @click="submitForm(row); dialogVisible = true">编辑</el-button>
            <el-button type="text" @click="handleDelete(row)" style="color:var(--el-color-danger)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <!-- 分页组件 -->
  <el-pagination
      v-model:current-page="pagination.pageNum"
      v-model:page-size="pagination.pageSize"
      :total="pagination.total"
      @current-change="fetchData"
      @size-change="fetchData"
      layout="total, sizes, prev, pager, next"
  />
  <!-- 新增/编辑 -->
  <el-dialog v-model="dialogVisible" :title="formModel.id ? '编辑会员' : '新增会员'">
    <el-form :model="formModel" label-width="120px">
      <el-form-item label="会员名称" required>
        <el-input v-model="formModel.name"/>
      </el-form-item>
      <el-form-item label="折扣率" required>
        <el-input-number
            v-model="formModel.discountRate"
            :min="0" :max="1"
            :step="0.05"
            precision="2"
        />
      </el-form-item>
      <el-form-item label="消费金额" required>
        <el-input-number
            v-model="formModel.pointsRequired"
            :min="0" :max="10000"
            :step="500"
            precision="2"
        />
      </el-form-item>
      <el-form-item label="所属门店" required>
        <el-select v-model="formModel.storeId">
          <el-option
              v-for="store in stores"
              :key="store"
              :label="store"
              :value="store"
          />
        </el-select>
      </el-form-item>

    </el-form>

    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="submitForm">确定</el-button>
    </template>
  </el-dialog>
</template>

<style scoped>
.member-type-container {
  padding: 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.header {
  margin-bottom: 20px;
}

.header h1 {
  font-size: 20px;
  color: #333;
  margin: 0;
}

.filter-container {
  margin-bottom: 20px;
}

.filter-form {
  display: flex;
  flex-wrap: wrap;
}

.table-container {
  margin-bottom: 20px;
}
</style>